package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.AppBanner;
import com.ruoyi.background.mapper.AppBannerMapper;
import com.ruoyi.background.service.IAppBannerService;
import com.ruoyi.background.utils.REDIS_KEY;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 小程序bannerService业务层处理
 *
 * @author susu
 * @date 2024-08-02
 */
@Service
public class AppBannerServiceImpl implements IAppBannerService {
    @Autowired
    private AppBannerMapper appBannerMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询小程序banner
     *
     * @param id 小程序banner主键
     * @return 小程序banner
     */
    @Override
    public AppBanner selectAppBannerById(Long id) {
        return appBannerMapper.selectAppBannerById(id);
    }

    /**
     * 查询小程序banner列表
     *
     * @param appBanner 小程序banner
     * @return 小程序banner
     */
    @Override
    public List<AppBanner> selectAppBannerList(AppBanner appBanner) {
        return appBannerMapper.selectAppBannerList(appBanner);
    }


    /**
     * 更新缓存的列表
     */
    @Async
    @Override
    public void updateBannerCacheList() {
        String key = REDIS_KEY.BANNER_LIST_KEY;
        List<AppBanner> bannerList = appBannerMapper.selectAppBannerList(new AppBanner());
        redisCache.deleteByKey(key);
        redisCache.setCacheList(key, bannerList);
        redisCache.expire(key, 60, TimeUnit.MINUTES);
    }


    /**
     * 新增小程序banner
     *
     * @param appBanner 小程序banner
     * @return 结果
     */
    @Override
    public int insertAppBanner(AppBanner appBanner) {
        appBanner.setCreateTime(DateUtils.getNowDate());
        appBanner.setCreateBy(SecurityUtils.getUsername());
        int i = appBannerMapper.insertAppBanner(appBanner);
        if (i > 0) {
            this.updateBannerCacheList();
        }
        return i;
    }

    /**
     * 修改小程序banner
     *
     * @param appBanner 小程序banner
     * @return 结果
     */
    @Override
    public int updateAppBanner(AppBanner appBanner) {
        appBanner.setUpdateTime(DateUtils.getNowDate());
        appBanner.setUpdateBy(SecurityUtils.getUsername());
        int i = appBannerMapper.updateAppBanner(appBanner);
        if (i > 0) {
            this.updateBannerCacheList();
        }
        return i;
    }

    /**
     * 批量删除小程序banner
     *
     * @param ids 需要删除的小程序banner主键
     * @return 结果
     */
    @Override
    public int deleteAppBannerByIds(Long[] ids) {
        int res = appBannerMapper.deleteAppBannerByIds(ids);
        if (res > 0) {
            this.updateBannerCacheList();
        }
        return res;
    }

    /**
     * 删除小程序banner信息
     *
     * @param id 小程序banner主键
     * @return 结果
     */
    @Override
    public int deleteAppBannerById(Long id) {
        int res = appBannerMapper.deleteAppBannerById(id);
        if (res > 0) {
            this.updateBannerCacheList();
        }
        return res;
    }
}
